package com.jtny.nytb.equipment.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jtny.component.contants.GenericConstants;
import com.jtny.component.core.util.PageUtils;
import com.jtny.component.core.util.QueryWrapperUtils;
import com.jtny.component.dto.CurrentUserDTO;
import com.jtny.component.entity.LocalHolder;
import com.jtny.component.entity.PageData;
import com.jtny.component.enums.GenericStatusEnum;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.Assert;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.CurrentUserUtils;
import com.jtny.nytb.equipment.dao.dataparam.DataParamTempDAO;
import com.jtny.nytb.equipment.dao.equipment.EquipmentInfoDAO;
import com.jtny.nytb.equipment.dao.model.ModelDAO;
import com.jtny.nytb.equipment.dao.model.ModelParamDAO;
import com.jtny.nytb.equipment.domain.dataparam.DataParamTempDO;
import com.jtny.nytb.equipment.domain.equipment.EquipmentInfoDO;
import com.jtny.nytb.equipment.domain.model.ModelDO;
import com.jtny.nytb.equipment.domain.model.ModelParamDO;
import com.jtny.nytb.equipment.dto.ParamBaseDTO;
import com.jtny.nytb.equipment.dto.dataparam.DataParamTempDTO;
import com.jtny.nytb.equipment.dto.model.ModelInfoDTO;
import com.jtny.nytb.equipment.enums.DataParamImportEnum;
import com.jtny.nytb.equipment.enums.DataParamRelationTypeEnum;
import com.jtny.nytb.equipment.errorcode.EquipmentModelErrorCodeEnum;
import com.jtny.nytb.equipment.service.EquipmentModelService;
import com.jtny.nytb.equipment.utils.RandomCodeGeneratorUtil;
import com.jtny.nytb.equipment.vo.excel.ExportExcelModelVO;
import com.jtny.nytb.equipment.vo.model.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 智慧能源中心 - zx
 * @package com.jtny.energymonitor.equipment.service.impl
 * @fileName EquipmentModelServiceImpl.java
 * @createTime 2023年09月15日 15:57
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
@AllArgsConstructor
public class EquipmentModelServiceImpl implements EquipmentModelService {
    private ModelParamDAO modelParamDAO;
    private ModelDAO modelDAO;
    private DataParamTempDAO dataParamTempDAO;
    private DataParamTempServiceImpl dataParamTempService;
    private RandomCodeGeneratorUtil randomCodeGeneratorUtil;

    /**
     * 设备模型参数分页
     *
     * @param equipmentModelQueryPageParamDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<EquipmentModelParamTempVO> queryPages(EquipmentModelQueryPageParamTempVO equipmentModelQueryPageParamDTO) throws ServiceException {
        // 封装查询条件QueryWrapper对象
        QueryWrapper<ModelParamDO> queryWrapper = QueryWrapperUtils.getQueryWrapper(equipmentModelQueryPageParamDTO);
        queryWrapper.orderByDesc("id");
        queryWrapper.ne("status", 9);
        // 分页查询用户信息
        Page<ModelParamDO> page = Page.of(equipmentModelQueryPageParamDTO.getPageNo(), equipmentModelQueryPageParamDTO.getPageSize());
        PageData<ModelParamDO> pageDataForDO = PageUtils.getPageData(modelParamDAO.selectPage(page, queryWrapper), equipmentModelQueryPageParamDTO);

        return pageDataForDO.transformData(EquipmentModelParamTempVO.class);
    }

    /**
     * 通过id查询单个设备模型对象
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public ModelTempVO getById(Long id) throws ServiceException {
        QueryWrapper<ModelDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id", id);
        ModelDO modelDO = modelDAO.selectOne(objectQueryWrapper);
        return BeanUtils.copyToObject(modelDO, ModelTempVO.class);
    }

    /**
     * 分页查询设备模型管理信息
     *
     * @param equipmentModelPageQueryParamDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<ModelTempVO> queryPage(EquipmentModelPageQueryParamTempVO equipmentModelPageQueryParamDTO) throws ServiceException {
        // 封装查询条件QueryWrapper对象
        QueryWrapper<ModelDO> queryWrapper = QueryWrapperUtils.getQueryWrapper(equipmentModelPageQueryParamDTO);
        queryWrapper.orderByDesc("id");
        queryWrapper.ne("status", 9);
        // 分页查询用户信息
        Page<ModelDO> page = Page.of(equipmentModelPageQueryParamDTO.getPageNo(), equipmentModelPageQueryParamDTO.getPageSize());
        PageData<ModelDO> pageDataForDO = PageUtils.getPageData(modelDAO.selectPage(page, queryWrapper), equipmentModelPageQueryParamDTO);

        return pageDataForDO.transformData(ModelTempVO.class);
    }

    /**
     * 修改设备模型信息
     *
     * @param modelDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public ModelTempVO update(ModelTempVO modelDTO) throws ServiceException {
        Assert.notNull(modelDTO, EquipmentModelErrorCodeEnum.PARAM_OBJECT_IS_NULL);
        // 将DTO对象的属性复制给DO对象
        ModelDO modelDO = BeanUtils.copyToObject(modelDTO, ModelDO.class);

        Integer status = modelDO.getStatus();
        if (GenericStatusEnum.DELETED.getValue().equals(status)) {
            deleteAll(modelDO.getId());
        } else {
            Assert.isTrue(checkName(modelDTO.getName(), modelDTO.getId()), EquipmentModelErrorCodeEnum.PARAM_OBJECT_NAME_REPEATED);
            modelDAO.updateById(modelDO);
        }
        return modelDTO;
    }

    /**
     * 设备模型参数保存
     *
     * @param equipmentModelParamDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelParamTempVO savaParam(EquipmentModelParamTempVO equipmentModelParamDTO) throws ServiceException {
        if (equipmentModelParamDTO == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.PARAM_OBJECT_PARAM_IS_NULL.getErrorCode(), EquipmentModelErrorCodeEnum.PARAM_OBJECT_PARAM_IS_NULL.getErrorMessage());
        }

        // 将DTO对象的属性复制给DO对象
        ModelParamDO modelParamDO = BeanUtils.copyToObject(equipmentModelParamDTO, ModelParamDO.class);

        // 保存操作
        modelParamDAO.insert(modelParamDO);

        // 将id设置给DTO对象
        equipmentModelParamDTO.setId(modelParamDO.getId());

        return equipmentModelParamDTO;
    }

    private void deleteAll(Long modelId) {
        modelDAO.deleteById(modelId);
        List<DataParamTempDO> dataParamTempDOS = dataParamTempDAO.selectList(Wrappers.<DataParamTempDO>lambdaQuery()
                .eq(DataParamTempDO::getRelationType, DataParamRelationTypeEnum.MODEL.getType())
                .eq(DataParamTempDO::getRelationId, modelId));
        if (CollUtil.isEmpty(dataParamTempDOS)) {
            return;
        }
        List<Long> dataParamTempIds = dataParamTempDOS.stream().map(DataParamTempDO::getId).collect(Collectors.toList());
        dataParamTempService.deleteAll(dataParamTempIds);
    }

    /**
     * 校验名称是否重复
     *
     * @param name 名称
     * @param id   主键ID
     * @return {@link Boolean}
     */
    private Boolean checkName(String name, Long id) {
        if (StringUtils.isBlank(name)) {
            return Boolean.TRUE;
        }
        if (ObjectUtil.isEmpty(id)) {
            return !modelDAO.exists(Wrappers.<ModelDO>lambdaQuery()
                    .eq(ModelDO::getName, name)
                    .ne(ModelDO::getStatus, GenericStatusEnum.DELETED.getValue()));
        } else {
            return !modelDAO.exists(Wrappers.<ModelDO>lambdaQuery()
                    .eq(ModelDO::getName, name)
                    .ne(ModelDO::getStatus, GenericStatusEnum.DELETED.getValue())
                    .ne(ModelDO::getId, id));
        }
    }

    /**
     * 通过单个设备模型参数id查询单个设备模型参数信息
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelParamTempVO getByIdParam(Long id) throws ServiceException {
        QueryWrapper<ModelParamDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id", id);
        ModelParamDO modelParamDO = modelParamDAO.selectOne(objectQueryWrapper);
        return BeanUtils.copyToObject(modelParamDO, EquipmentModelParamTempVO.class);
    }

    /**
     * 修改设备模型参数信息
     *
     * @param equipmentModelParamDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelParamTempVO updateParam(EquipmentModelParamTempVO equipmentModelParamDTO) throws ServiceException {
        if (equipmentModelParamDTO == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.PARAM_OBJECT_PARAM_IS_NULL.getErrorCode(), EquipmentModelErrorCodeEnum.PARAM_OBJECT_PARAM_IS_NULL.getErrorMessage());
        }

        // 将DTO对象的属性复制给DO对象
        ModelParamDO modelParamDO = BeanUtils.copyToObject(equipmentModelParamDTO, ModelParamDO.class);

        modelParamDAO.updateById(modelParamDO);
        return equipmentModelParamDTO;
    }

    /**
     * 保存设备模型信息
     *
     * @param modelDTO
     * @return
     * @throws ServiceException
     */
    @Override
    public ModelTempVO save(ModelTempVO modelDTO) throws ServiceException {
        Assert.notNull(modelDTO, EquipmentModelErrorCodeEnum.PARAM_OBJECT_IS_NULL);
        Assert.isTrue(checkName(modelDTO.getName(), null), EquipmentModelErrorCodeEnum.PARAM_OBJECT_NAME_REPEATED);
        // 将DTO对象的属性复制给DO对象
        ModelDO modelDO = BeanUtils.copyToObject(modelDTO, ModelDO.class);
        // 保存操作
        modelDAO.insert(modelDO);
        // 将id设置给DTO对象
        modelDTO.setId(modelDO.getId());
        return modelDTO;
    }


    @Override
    public PageData<EquipmentModelParamVO> queryPageParam(EquipmentModelQueryPageParamVO equipmentModelQueryPageParamVO) throws ServiceException {
        //校验参数处理
        if (equipmentModelQueryPageParamVO == null) {
            equipmentModelQueryPageParamVO = new EquipmentModelQueryPageParamVO();
            equipmentModelQueryPageParamVO.setPageNo(GenericConstants.DEFAULT_PAGE_NO);
            equipmentModelQueryPageParamVO.setPageSize(GenericConstants.DEFAULT_PAGE_SIZE);
        }

        //分页查询设备模型信息
        EquipmentModelQueryPageParamTempVO equipmentModelQueryPageParamDTO = BeanUtils.copyToObject(equipmentModelQueryPageParamVO, EquipmentModelQueryPageParamTempVO.class);
        PageData<EquipmentModelParamTempVO> pageDataForDTO = this.queryPages(equipmentModelQueryPageParamDTO);
        if (pageDataForDTO == null || CollUtil.isEmpty(pageDataForDTO.getData())) {
            return new PageData<>();
        }

        Collection<EquipmentModelParamTempVO> equipmentModelParamDTOCollection = pageDataForDTO.getData();
        List<EquipmentModelParamVO> equipmentModelParamVOList = new ArrayList<>(equipmentModelParamDTOCollection.size());
        equipmentModelParamDTOCollection.forEach(equipmentModelParamDTO -> {
            EquipmentModelParamVO equipmentModelParamVO = this.getEquipmentModelParamVOByDTO(equipmentModelParamDTO);
            equipmentModelParamVOList.add(equipmentModelParamVO);
        });

        PageData<EquipmentModelParamVO> pageDataForVO = pageDataForDTO.transformExcludeData(EquipmentModelParamVO.class);
        pageDataForVO.setData(equipmentModelParamVOList);

        return pageDataForVO;
    }

    private EquipmentModelParamVO getEquipmentModelParamVOByDTO(EquipmentModelParamTempVO equipmentModelParamDTO) {
        return BeanUtils.copyToObject(equipmentModelParamDTO, EquipmentModelParamVO.class);
    }

    /**
     * 查询分页设备模型信息
     *
     * @param equipmentModelPageQueryParamVO
     * @return
     * @throws ServiceException
     */
    @Override
    public PageData<EquipmentModelVO> queryPage(EquipmentModelPageQueryParamVO equipmentModelPageQueryParamVO) throws ServiceException {
        //分页查询设备模型信息
        EquipmentModelPageQueryParamTempVO equipmentModelPageQueryParamDTO = BeanUtils.copyToObject(equipmentModelPageQueryParamVO, EquipmentModelPageQueryParamTempVO.class);
        PageData<ModelTempVO> pageDataForDTO = this.queryPage(equipmentModelPageQueryParamDTO);
        if (pageDataForDTO == null || CollUtil.isEmpty(pageDataForDTO.getData())) {
            return new PageData<>();
        }

        Collection<ModelTempVO> modelDTOCollection = pageDataForDTO.getData();
        List<EquipmentModelVO> userVOList = new ArrayList<>(modelDTOCollection.size());
        modelDTOCollection.forEach(equipmentModelDTO -> {
            EquipmentModelVO equipmentModelVO = this.getEquipmentModelVOByDTO(equipmentModelDTO);
            userVOList.add(equipmentModelVO);
        });

        PageData<EquipmentModelVO> pageDataForVO = pageDataForDTO.transformExcludeData(EquipmentModelVO.class);
        pageDataForVO.setData(userVOList);

        return pageDataForVO;
    }

    private EquipmentModelVO getEquipmentModelVOByDTO(ModelTempVO modelDTO) {
        return BeanUtils.copyToObject(modelDTO, EquipmentModelVO.class);
    }

    /**
     * 导出list
     *
     * @param equipmentModelPageQueryParamVO 设备模型页面查询参数 VO
     * @return {@link Collection}<{@link EquipmentModelVO}>
     */
    @Override
    public Collection<EquipmentModelVO> exportList(EquipmentModelPageQueryParamVO equipmentModelPageQueryParamVO) {
        equipmentModelPageQueryParamVO.setPageNo(1);
        equipmentModelPageQueryParamVO.setPageSize(Integer.MAX_VALUE);
        PageData<EquipmentModelVO> equipmentModelVOPageData = queryPage(equipmentModelPageQueryParamVO);
        return equipmentModelVOPageData.getData();
    }

    /**
     * 设备模型信息修改操作
     *
     * @param equipmentModelVO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelVO update(EquipmentModelVO equipmentModelVO) throws ServiceException {
        Long equipmentModelId = equipmentModelVO.getId();
        if (equipmentModelId == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.PARAM_MODEL_ID_IS_NULL.getErrorCode(), EquipmentModelErrorCodeEnum.PARAM_MODEL_ID_IS_NULL.getErrorMessage());
        }

        //通过用户id获取设备模型DTO对象
        ModelTempVO modelDTO = this.getById(equipmentModelId);
        if (modelDTO == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(), EquipmentModelErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }

        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        if (CollectionUtil.isEmpty(equipmentModelVO.getRuleIdList())) {
            //封装待修改的设备模型DTO对象
            BeanUtils.copyObject(equipmentModelVO, modelDTO);
            CurrentUserUtils.setUpdateDefaultField(modelDTO, currentUserDTO);
            //修改操作
            this.update(modelDTO);
        } else {
            //使用正则表达式过滤掉非数字的字符
            String equipmentCodeRule = equipmentModelVO.getEquipmentCodeRule();
            if (StrUtil.isNotBlank(equipmentCodeRule)) {
                List<String> ruleIdList = Arrays.asList(equipmentCodeRule.split(StrUtil.COMMA));
                dataParamTempService.backDataParamTemp(equipmentModelVO.getId());
                dataParamTempService.moveDataParamTempByRuleDictIds(equipmentModelVO.getId(), ruleIdList);
            }
        }

        return equipmentModelVO;
    }

    /**
     * 设备模型参数信息添加操作
     *
     * @param equipmentModelParamVO
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelParamVO savaParam(EquipmentModelParamVO equipmentModelParamVO) throws ServiceException {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();

        //封装DTO对象
        EquipmentModelParamTempVO equipmentModelParamDTO = BeanUtils.copyToObject(equipmentModelParamVO, EquipmentModelParamTempVO.class);
        equipmentModelParamDTO.setStatus(GenericStatusEnum.NORMAL.getValue());
        CurrentUserUtils.setCreateDefaultField(equipmentModelParamDTO, currentUserDTO);
        //保存设备模型参数信息
        EquipmentModelParamTempVO equipmentModelParamDTOFirst = this.savaParam(equipmentModelParamDTO);

        return BeanUtils.copyToObject(equipmentModelParamDTOFirst, EquipmentModelParamVO.class);
    }

    /**
     * 设备模型参数信息删除操作
     *
     * @param id
     * @throws ServiceException
     */
    @Override
    public void deleteModelParam(Long id) throws ServiceException {
        EquipmentModelParamTempVO equipmentModelParamDTO = this.getByIdParam(id);
        if (equipmentModelParamDTO == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.OBJECT_PARAM_NOT_FIND.getErrorCode(), EquipmentModelErrorCodeEnum.OBJECT_PARAM_NOT_FIND.getErrorMessage());
        }

        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的用户DTO对象
        equipmentModelParamDTO.setStatus(GenericStatusEnum.DELETED.getValue());
        CurrentUserUtils.setUpdateDefaultField(equipmentModelParamDTO, currentUserDTO);

        //修改操作
        this.updateParam(equipmentModelParamDTO);

    }

    /**
     * 通过设备模型id查询单个设备模型信息
     *
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public EquipmentModelVO detailsModel(Long id) throws ServiceException {
        ModelTempVO modelDTO = this.getById(id);
        return BeanUtils.copyToObject(modelDTO, EquipmentModelVO.class);
    }

    /**
     * 设备模型信息删除操作
     *
     * @param id
     * @throws ServiceException
     */
    @Override
    public void deleteModel(Long id) throws ServiceException {

        //通过设备模型id获取设备模型DTO对象
        ModelTempVO modelDTO = this.getById(id);
        if (modelDTO == null) {
            throw new ServiceException(EquipmentModelErrorCodeEnum.OBJECT_NOT_FIND.getErrorCode(), EquipmentModelErrorCodeEnum.OBJECT_NOT_FIND.getErrorMessage());
        }

        //获取当前登录用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        //封装待修改的设备模型DTO对象
        modelDTO.setStatus(GenericStatusEnum.DELETED.getValue());
        CurrentUserUtils.setUpdateDefaultField(modelDTO, currentUserDTO);

        //修改操作
        this.update(modelDTO);

    }

    @Override
    public EquipmentModelVO save(EquipmentModelVO equipmentModelVO) throws ServiceException {
        //获取当前用户
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();

        String code = generateCode(equipmentModelVO);

        //封装DTO对象
        ModelTempVO modelDTO = BeanUtils.copyToObject(equipmentModelVO, ModelTempVO.class);
        modelDTO.setCode(code);
        modelDTO.setStatus(GenericStatusEnum.NORMAL.getValue());
        CurrentUserUtils.setCreateDefaultField(modelDTO, currentUserDTO);
        //保存设备模型信息
        ModelTempVO modelDTOFirst = this.save(modelDTO);


        //使用正则表达式过滤掉非数字的字符
        String equipmentCodeRule = equipmentModelVO.getEquipmentCodeRule();
        if (StrUtil.isNotBlank(equipmentCodeRule)) {
            List<String> ruleIdList = Arrays.asList(equipmentCodeRule.split(StrUtil.COMMA));
            dataParamTempService.moveDataParamTempByRuleDictIds(modelDTOFirst.getId(), ruleIdList);
        }
        return BeanUtils.copyToObject(modelDTOFirst, EquipmentModelVO.class);
    }

    private String generateCode(EquipmentModelVO equipmentModelVO) {
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        StringBuilder codeBuilder = new StringBuilder();
        String userCode = currentUserDTO.getCode();
        String energyClassification =equipmentModelVO.getEnergyClassification();
        String brand = equipmentModelVO.getBrand();
        String type = equipmentModelVO.getType();
        String randomNumber = randomCodeGeneratorUtil.generateUniqueFourDigitCode();
        codeBuilder.append(userCode).append('_').append(energyClassification).append('_').append(brand).append('_').append(type).append('_').append(randomNumber);
        return codeBuilder.toString();
    }

    private List<EquipmentModelVO> convertToRequestVO(List<ExportExcelModelVO> excelVOs) {
        return excelVOs.stream()
                .map(excelVO -> {
                    EquipmentModelVO requestVO = new EquipmentModelVO();
                    requestVO.setCode(excelVO.getCode());
                    requestVO.setName(excelVO.getName());
                    requestVO.setType(excelVO.getType());
                    requestVO.setModel(excelVO.getModel());
                    requestVO.setBrand(excelVO.getBrand());
                    requestVO.setDescription(excelVO.getDescription());
                    return requestVO;
                })
                .collect(Collectors.toList());
    }
    private List<EquipmentInfoDO> convertToRequestDO(List<EquipmentModelVO> excelVOs) {
        return excelVOs.stream()
                .map(excelVO -> {
                    EquipmentInfoDO requestVO = new EquipmentInfoDO();
                    requestVO.setCode(excelVO.getCode());
                    requestVO.setName(excelVO.getName());
                    requestVO.setType(excelVO.getType());
                    requestVO.setModel(excelVO.getModel());
                    requestVO.setBrand(excelVO.getBrand());
                    requestVO.setDescription(excelVO.getDescription());
                    return requestVO;
                })
                .collect(Collectors.toList());
    }

    private EquipmentInfoDAO equipmentInfoDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(List<ExportExcelModelVO> exportExcelModelVOS) throws ServiceException {
        if (CollUtil.isEmpty(exportExcelModelVOS)) {
            throw new ServiceException("当前没有模型导入");
        }

        List<EquipmentModelVO> equipmentModelVOS = convertToRequestVO(exportExcelModelVOS);
        //excel重复
        Set<String> names = equipmentModelVOS.stream().map(EquipmentModelVO::getCode).collect(Collectors.toSet());
        if (names.size() < equipmentModelVOS.size()) {
            throw new ServiceException("编号重复！");
        }

        List<EquipmentInfoDO> equipmentInfoDOS =convertToRequestDO(equipmentModelVOS);

        for(EquipmentInfoDO equipmentInfoDO:equipmentInfoDOS){
            LambdaQueryWrapper<EquipmentInfoDO> wrapper=new LambdaQueryWrapper<EquipmentInfoDO>()
                    .eq(EquipmentInfoDO::getCode,equipmentInfoDO.getCode());
            if(!equipmentInfoDAO.selectList(wrapper).isEmpty()) throw new ServiceException("不可重复导入");
        }

        try{
            equipmentInfoDAO.insertBatch(equipmentInfoDOS);
        }catch (Exception e){
            throw new ServiceException("导入失败");
        }
    }

    @Override
    public Map<String, ModelInfoDTO> mapModelByNameList(DataParamImportEnum type, List<String> modelNameList) {
        if (CollUtil.isEmpty(modelNameList)) {
            return new HashMap<>();
        }
        return mapModelByNameList2(type.getCodeRuleTypeEnum().getType(), modelNameList);
    }

    public Map<String, ModelInfoDTO> mapModelByNameList2(String type, List<String> modelNameList)
            throws ServiceException {
        Map<String, ModelInfoDTO> map = new HashMap<>();

        if (CollUtil.isNotEmpty(modelNameList)) {
            List<ModelDO> modelDOList = modelDAO.selectList(Wrappers.<ModelDO>lambdaQuery()
                    .in(ModelDO::getName, modelNameList)
                    .ne(ModelDO::getStatus, GenericStatusEnum.DELETED.getValue()));

            if (CollUtil.isEmpty(modelDOList)) {
                return new HashMap<>();
            }

            List<ModelInfoDTO> modelInfoDTOList = BeanUtils.copyToList(modelDOList, ModelInfoDTO.class);
            List<Long> modelIds = modelInfoDTOList.stream().map(ModelInfoDTO::getId).collect(Collectors.toList());
            Map<Long, List<DataParamTempDTO>> mapDataParamTempDTOList = mapDataParamTempDTOList(modelIds,type);
            Map<Long, List<ParamBaseDTO>> longListMap = mapParamDTOList(modelIds);
            for (ModelInfoDTO modelInfoDTO : modelInfoDTOList) {
                List<DataParamTempDTO> dataParamTempDTOTwoList = mapDataParamTempDTOList.get(modelInfoDTO.getId());
                List<ParamBaseDTO> paramBaseDTOTwoList = longListMap.get(modelInfoDTO.getId());
                modelInfoDTO.setDataParamTempList(dataParamTempDTOTwoList);
                modelInfoDTO.setParamList(paramBaseDTOTwoList);
                map.put(modelInfoDTO.getName(), modelInfoDTO);
            }
        }
        return map;
    }
    private Map<Long, List<ParamBaseDTO>> mapParamDTOList(List<Long> modelIdList) {
        Map<Long, List<ParamBaseDTO>> result = new HashMap<>();
        if (CollUtil.isEmpty(modelIdList)) {
            return result;
        }
        List<ModelParamDO> modelParamDOS = modelParamDAO.selectList(Wrappers.<ModelParamDO>lambdaQuery()
                .in(ModelParamDO::getModelId, modelIdList));
        if (CollUtil.isEmpty(modelParamDOS)) {
            return result;
        }
        for (ModelParamDO modelParamDO : modelParamDOS) {
            Long modelId = modelParamDO.getModelId();
            List<ParamBaseDTO> paramBaseDTOTwoList = result.get(modelId);
            if (paramBaseDTOTwoList == null) {
                paramBaseDTOTwoList = new ArrayList<>();
            }
            paramBaseDTOTwoList.add(BeanUtils.copyToObject(modelParamDO, ParamBaseDTO.class));
            result.put(modelId, paramBaseDTOTwoList);
        }
        return result;
    }

    private Map<Long, List<DataParamTempDTO>> mapDataParamTempDTOList(List<Long> modelIdList, String type) {
        Map<Long, List<DataParamTempDTO>> result = new HashMap<>();
        List<DataParamTempDTO> tempList = new ArrayList<>();

        if (CollUtil.isEmpty(modelIdList)) {
            return result;
        }
        if (type.equals(DataParamImportEnum.MODEL.getCodeRuleTypeEnum().getType())){
            List<DataParamTempDO> dataParamTempDOS = dataParamTempDAO.selectList(Wrappers.<DataParamTempDO>lambdaQuery()
                    .eq(DataParamTempDO::getRelationType, DataParamRelationTypeEnum.MODEL.getType())
                    .in(DataParamTempDO::getRelationId, modelIdList));
            if (CollUtil.isEmpty(dataParamTempDOS)) {
                return result;
            }
            dataParamTempDOS.forEach(item -> {
                DataParamTempDTO byId = BeanUtils.copyToObject(dataParamTempService.getById(item.getId()),DataParamTempDTO.class);
                tempList.add(byId);
            });
        }
        if (type.equals(DataParamImportEnum.METER_MODEL.getCodeRuleTypeEnum().getType())){
            List<DataParamTempDO> dataParamTempDOS = dataParamTempDAO.selectList(Wrappers.<DataParamTempDO>lambdaQuery()
                    .eq(DataParamTempDO::getRelationType, DataParamRelationTypeEnum.METER_MODEL.getType())
                    .in(DataParamTempDO::getRelationId, modelIdList));
            if (CollUtil.isEmpty(dataParamTempDOS)) {
                return result;
            }
            dataParamTempDOS.forEach(item -> {
                DataParamTempDTO byId =BeanUtils.copyToObject(dataParamTempService.getById(item.getId()),DataParamTempDTO.class) ;
                tempList.add(byId);
            });
        }
        return tempList.stream().collect(Collectors.groupingBy(DataParamTempDTO::getRelationId));
    }
}
